package com.leo.mybatis;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leo.mybatis.mapper.IOrderMapper;
import com.leo.mybatis.mapper.IUserMapper;
import com.leo.mybatis.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

public class PageHelperTest {

    private IUserMapper userMapper;
    private SqlSession sqlSession;

    @Before
    public void before() throws IOException {

        //使用InputStream或者Reader都可以
        //InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        //开启事务自动提交
        sqlSession = sqlSessionFactory.openSession(true);
        userMapper = sqlSession.getMapper(IUserMapper.class);
    }

    @After
    public void after(){
        sqlSession.close();
    }

    /**
     * 可以看到PageHelper 将 原有的MappedStatement 拆分并重新创建了2个MappedStatement
     *
     * SELECT count(*) FROM user
     * select * from user limit ?,?
     */
    @Test
    public void pageHelperTest(){

        PageHelper.startPage(1,2);
        List<User> users = userMapper.selectUser();

        PageInfo<User> pageInfo = new PageInfo<>(users);

        System.out.println("总条数："+pageInfo.getTotal());
        System.out.println("总页数："+pageInfo.getPages());
        System.out.println("当前页："+pageInfo.getPageNum());
        System.out.println("每页显示的条数："+pageInfo.getPageSize());


    }
}
